package com.paipaideli.ui.price;

/**
 * Created by 出价 on 2019/8/31.
 */

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.PullRefreshView;
import com.paipaideli.common.utils.ImageUtil;
import com.paipaideli.common.utils.StringUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.common.views.CircleImageView;
import com.paipaideli.ui.price.bean.HistoryBean;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class HistoryActivity extends BasePresenterActivity {

    Unbinder unbinder;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.history_recycleview)
    PullRefreshView history_recycleview;

    private String goodId;
    private String token;

    private List<HistoryBean.Data> msgList = new ArrayList<>();
    private HistoryAdapter adapter;

    @Override
    public int getContentView() {
        return R.layout.activity_history;
    }

    @Override
    public void initView() {
        unbinder = ButterKnife.bind(this);
        top_title.setText("历史记录");
        //返回
        top_back.setOnClickListener(o ->
                finish()
        );

        goodId = getIntent().getStringExtra("id");
        token = getIntent().getStringExtra("token");

        history_recycleview.setEnableRefresh(false);
        history_recycleview.setEnableLoadmore(false);
        adapter = new HistoryAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        history_recycleview.setLayoutManager(linearLayoutManager);
        history_recycleview.setAdapter(adapter);

        //初始化消息数据
        initMsg();

    }

    //初始化数据
    private void initMsg() {
        history_recycleview.showLoading();
        JSONObject requestData = new JSONObject();
        try {
            requestData.put("aid", goodId);
        } catch (Exception e) {
            e.getMessage();
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
        Api.getInstanceGson().bidrecord(body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(request -> {
                    history_recycleview.finishLoading();
                    if (request.code.equals("200")) {
                        msgList.addAll(request.data);
                        if (msgList.size() == 0){
                            history_recycleview.showEmpty("");
                        }
                        if (adapter != null) {
                            adapter.notifyDataSetChanged();
                        }
                    } else {
                        ToastUtil.show(request.msg);
                    }
                }, throwable -> {
                    history_recycleview.finishLoading();
                    Log.e(TAG, "error", throwable);
                });
    }

    public class HistoryAdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(HistoryActivity.this).inflate(R.layout.item_price, null);
            inflate.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            ViewHolder viewHolder = (ViewHolder) holder;
            //判断消息类型 如果是收到的消息 显示左边的消息布局，隐藏右边的消息布局
            viewHolder.lin_price_look.setVisibility(View.GONE);
            viewHolder.tvPriceTime.setText(msgList.get(position).actionTimeStr);
            if (!msgList.get(position).userId.equals(token)) {
                viewHolder.linLeft.setVisibility(View.VISIBLE);
                viewHolder.linRight.setVisibility(View.GONE);
                ImageUtil.displayImage(msgList.get(position).avatar, viewHolder.imagePriceLeft);
                viewHolder.tvPriceLeftName.setText(msgList.get(position).userName);
                viewHolder.tvPriceLeftPrice.setText(" 出价" + msgList.get(position).curPrice + "元，将获得" + msgList.get(position).bounty + "元的奖励金 ");
                //判断消息类型 如果是发送的消息 显示右边的消息布局，隐藏左边的消息布局
            } else if (msgList.get(position).userId.equals(token)) {
                viewHolder.linLeft.setVisibility(View.GONE);
                viewHolder.linRight.setVisibility(View.VISIBLE);
                ImageUtil.displayImage(msgList.get(position).avatar, viewHolder.imagePriceRight);
                viewHolder.tvPriceRightName.setText(msgList.get(position).userName);
                viewHolder.tvPriceRightPrice.setText(" 出价" + msgList.get(position).curPrice + "元，将获得" + msgList.get(position).bounty + "元的奖励金 ");
            }
            viewHolder.lin_price_success.setVisibility(View.GONE);
        }

        @Override
        public int getItemCount() {
            return msgList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.lin_price_look)
            LinearLayout lin_price_look;
            @BindView(R.id.tv_price_time)
            TextView tvPriceTime;
            @BindView(R.id.image_price_left)
            CircleImageView imagePriceLeft;
            @BindView(R.id.tv_price_left_name)
            TextView tvPriceLeftName;
            @BindView(R.id.tv_price_left_price)
            TextView tvPriceLeftPrice;
            @BindView(R.id.lin_left)
            LinearLayout linLeft;
            @BindView(R.id.image_price_right)
            CircleImageView imagePriceRight;
            @BindView(R.id.tv_price_right_name)
            TextView tvPriceRightName;
            @BindView(R.id.tv_price_right_price)
            TextView tvPriceRightPrice;
            @BindView(R.id.lin_right)
            LinearLayout linRight;
            @BindView(R.id.lin_price_success)
            LinearLayout lin_price_success;

            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
